package org.xtext.runtime.formula;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.xtext.runtime.term.ITerm;
import org.xtext.runtime.term.Variable;
import org.xtext.runtime.util.Bindings;
import org.xtext.runtime.util.ILogicVisitor;

public class MessageEvent implements Event {
	private String performative;
	private ITerm sender;
	private Predicate content;
	
	public MessageEvent(String performative, ITerm sender, Predicate content) {
		this.performative = performative;
		this.sender = sender;
		this.content = content;
	}

	public String signature() {
		return "@message:"+performative+"/"+content.signature();
	}
	
	public String toString() {
		return "@message(" + performative + "," + sender + "," + content + ")";
	}
	
	public String performative() {
		return performative;
	}
	
	public ITerm sender() {
		return sender;
	}
	
	public Predicate content() {
		return content;
	}

	@Override
	public IFormula copy() {
		return new MessageEvent(performative, sender.copy(), content.copy());
	}

	@Override
	public Set<Variable> variables() {
		Set<Variable> variables = new HashSet<Variable>();
		if (sender instanceof Variable) {
			variables.add((Variable) sender); 
		}
		variables.addAll(content.variables());
		return variables;
	}

	@Override
	public IFormula apply(Bindings bindings) {
		sender = sender.apply(bindings);
		content = content.apply(bindings);
		return this;
	}

	@Override
	public IFormula copy(Map<String, Variable> vars) {
		return new MessageEvent(performative, sender.copy(vars), content.copy(vars));
	}

	@Override
	public boolean hasFreeVariables() {
		if (sender instanceof Variable && ((Variable) sender).isFree()) return true; 
		return content.hasFreeVariables();
	}

	@Override
	public Object accept(ILogicVisitor visitor) {
		return null;
	}

	@Override
	public Object source() {
		return null;
	}
}
